package com.ruoyi.smart.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.smart.domain.SmartProductTestWaveform;
import com.ruoyi.smart.mapper.SmartProductTestMapper;
import com.ruoyi.smart.domain.SmartProductTest;
import com.ruoyi.smart.service.ISmartProductTestService;

/**
 * 产品测试Service业务层处理
 * 
 * @author minwei
 * @date 2022-06-20
 */
@Service
public class SmartProductTestServiceImpl implements ISmartProductTestService 
{
    @Autowired
    private SmartProductTestMapper smartProductTestMapper;

    /**
     * 查询产品测试
     * 
     * @param id 产品测试主键
     * @return 产品测试
     */
    @Override
    public SmartProductTest selectSmartProductTestById(Long id)
    {
        return smartProductTestMapper.selectSmartProductTestById(id);
    }

    /**
     * 查询产品测试列表
     * 
     * @param smartProductTest 产品测试
     * @return 产品测试
     */
    @Override
    public List<SmartProductTest> selectSmartProductTestList(SmartProductTest smartProductTest)
    {
        return smartProductTestMapper.selectSmartProductTestList(smartProductTest);
    }

    /**
     * 新增产品测试
     * 
     * @param smartProductTest 产品测试
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSmartProductTest(SmartProductTest smartProductTest)
    {
        int rows = smartProductTestMapper.insertSmartProductTest(smartProductTest);
        insertSmartProductTestWaveform(smartProductTest);
        return rows;
    }

    /**
     * 修改产品测试
     * 
     * @param smartProductTest 产品测试
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSmartProductTest(SmartProductTest smartProductTest)
    {
        smartProductTestMapper.deleteSmartProductTestWaveformByProductTestId(smartProductTest.getId());
        insertSmartProductTestWaveform(smartProductTest);
        return smartProductTestMapper.updateSmartProductTest(smartProductTest);
    }

    /**
     * 批量删除产品测试
     * 
     * @param ids 需要删除的产品测试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSmartProductTestByIds(Long[] ids)
    {
        smartProductTestMapper.deleteSmartProductTestWaveformByProductTestIds(ids);
        return smartProductTestMapper.deleteSmartProductTestByIds(ids);
    }

    /**
     * 删除产品测试信息
     * 
     * @param id 产品测试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSmartProductTestById(Long id)
    {
        smartProductTestMapper.deleteSmartProductTestWaveformByProductTestId(id);
        return smartProductTestMapper.deleteSmartProductTestById(id);
    }

    /**
     * 新增产品波形数据信息
     * 
     * @param smartProductTest 产品测试对象
     */
    public void insertSmartProductTestWaveform(SmartProductTest smartProductTest)
    {
        List<SmartProductTestWaveform> smartProductTestWaveformList = smartProductTest.getSmartProductTestWaveformList();
        Long id = smartProductTest.getId();
        if (StringUtils.isNotNull(smartProductTestWaveformList))
        {
            List<SmartProductTestWaveform> list = new ArrayList<SmartProductTestWaveform>();
            for (SmartProductTestWaveform smartProductTestWaveform : smartProductTestWaveformList)
            {
                smartProductTestWaveform.setProductTestId(id);
                list.add(smartProductTestWaveform);
            }
            if (list.size() > 0)
            {
                smartProductTestMapper.batchSmartProductTestWaveform(list);
            }
        }
    }
}
